<?php if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) ob_start('ob_gzhandler'); else ob_start(); ?>
<?php
/*
 * Copyright 2009 Eat Local Food, LLC
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */
//paths for GWTCommerce stuff
$include_path = ini_get('include_path');
ini_set('include_path', 'php:' . $include_path);
      
require_once('includes/classes/JSON.php');
require_once('includes/components/ComponentController.php');
require_once('includes/functions/functions.php');
require_once('includes/functions/cache_functions.php');

$formAction = HTTPS_SERVER . 'index.php';
$hidden_parameters = '';

//Used when information has gone through a 3rd Party Process Payment
$gwtAction = $_REQUEST['gwt_action'];
if ($gwtAction == 'ProcessPayment') 
{
	$value = array();
	//reverse the 'gwt_' and other request parameters so that they are usable in Components and Modules
	//when finished, the parameters specific to the 3rd party will start with 'gwt_' and the parameters
	//specific to gwt commerce will have the 'gwt_' removed.
	foreach ($_REQUEST as $reqkey => $reqvalue)
	{
		$pos = strpos($reqkey, 'gwt_');
		if ($pos === false || $pos != 0) {
			$value['gwt_' . $reqkey] = $reqvalue;
		}
		else {
			$value[substr($reqkey, 4)] = $reqvalue;
		}
	}
	$value['token'] = CheckoutComponent;
	$value['third_party_payment_process'] = true;
	$value['handle'] = 'CheckoutComponent';
	$action = prepare_input($value['action']);
	$value['action'] = $action;

	$rvalue = actionComponent($value, $action);
	$errorCode = (int)$rvalue{'CheckoutComponent'}{0}{'error_code'};
	$action = $rvalue{'CheckoutComponent'}{0}{'action_completed'}= $action;
	$orderId = (int)$rvalue{'CheckoutComponent'}{0}{'payment'}{0}{'orderId'};
	
	$codesvr = $_GET['gwt_codesvr'];
	if ($codesvr != null && strlen($codesvr) > 0) {
		$formAction .= '?gwt.codesvr=' . $codesvr;
	}

	$hidden_parameters = '';
	if ($errorCode == 0  && $action == 'ProcessPayment')
	{
		$formAction .= '#OrderComponent--'.$orderId;
		foreach ($_REQUEST as $reqkey => $reqvalue)
		{
			if ($reqkey == 'gwt_accountId') {
				$hidden_parameters .= '<input type="hidden" name="accountId" VALUE="'.$reqvalue.'">';
			}
			else if ($reqkey == 'gwt_accountSession') {
				$hidden_parameters .= '<input type="hidden" name="accountSession" VALUE="'.$reqvalue.'">';
			}
		}
	}
	else
	{
		foreach ($_REQUEST as $reqkey => $reqvalue)
		{
			$hidden_parameters .= '<input type="hidden" name="'.$reqkey.'" VALUE="'.$reqvalue.'">';
		}
	}
}
?>
<!DOCTYPE HTML PUBLIC >
<html>
<body  LANGUAGE="javascript">
<form id="form1" action="<?php echo $formAction?>" style="display: none;" method="post">
<?php echo $hidden_parameters?>
</form>
<script type="text/javascript">
function myfunc () {
var frm = document.getElementById("form1");
frm.submit();
}
window.onload = myfunc;
</script>
</body>
</html>
